home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / RKMLibsPrgs / graphics_libraries / primitives / RGBBoxes.c < prev    next >
C/C++ Source or Header  |  1992-09-03  |  10KB  |  321 lines

  1. ;/*  RGBBoxes.c simple ViewPort example -- works with 1.3 and Release 2
  2. LC -b1 -cfistq -v -y -j73 RGBBoxes.c
  3. Blink FROM LIB:c.o,RGBBoxes.o TO RGBBoxes LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit ;
  5.  
  6.  
  7. Copyright (c) 1992 Commodore-Amiga, Inc.
  8.  
  9. This example is provided in electronic form by Commodore-Amiga, Inc. for
  10. use with the "Amiga ROM Kernel Reference Manual: Libraries", 3rd Edition,
  11. published by Addison-Wesley (ISBN 0-201-56774-1).
  12.  
  13. The "Amiga ROM Kernel Reference Manual: Libraries" contains additional
  14. information on the correct usage of the techniques and operating system
  15. functions presented in these examples.  The source and executable code
  16. of these examples may only be distributed in free electronic form, via
  17. bulletin board or as part of a fully non-commercial and freely
  18. redistributable diskette.  Both the source and executable code (including
  19. comments) must be included, without modification, in any copy.  This
  20. example may not be published in printed form or distributed with any
  21. commercial product.  However, the programming techniques and support
  22. routines set forth in these examples may be used in the development
  23. of original executable software products for Commodore Amiga computers.
  24.  
  25. All other rights reserved.
  26.  
  27. This example is provided "as-is" and is subject to change; no
  28. warranties are made.  All use is at your own risk. No liability or
  29. responsibility is assumed.
  30.  
  31. */
  32.  
  33. #include <exec/types.h>
  34. #include <graphics/gfx.h>
  35. #include <graphics/gfxbase.h>
  36. #include <graphics/gfxmacros.h>
  37. #include <graphics/copper.h>
  38. #include <graphics/view.h>
  39. #include <graphics/displayinfo.h>
  40. #include <graphics/gfxnodes.h>
  41. #include <graphics/videocontrol.h>
  42. #include <libraries/dos.h>
  43. #include <utility/tagitem.h>
  44.  
  45. #include <clib/graphics_protos.h>
  46. #include <clib/exec_protos.h>
  47. #include <clib/dos_protos.h>
  48.  
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51.  
  52. #define DEPTH 2     /*  The number of bitplanes.  */
  53. #define WIDTH 640   /*  Nominal width and height  */
  54. #define HEIGHT 400  /*  used in 1.3.              */
  55.  
  56. #ifdef LATTICE
  57. int CXBRK(void)    { return(0); }  /* Disable Lattice CTRL/C handling */
  58. int chkabort(void) { return(0); }  /* really */
  59. #endif
  60.  
  61. VOID drawFilledBox(WORD , WORD );  /* Function prototypes */
  62. VOID cleanup(int );
  63. VOID fail(STRPTR);
  64.  
  65. struct GfxBase *GfxBase = NULL;
  66.  
  67. /*  Construct a simple display.  These are global to make freeing easier.   */
  68. struct View view, *oldview=NULL;  /*  Pointer to old View we can restore it.*/
  69. struct ViewPort viewPort = { 0 };
  70. struct BitMap bitMap = { 0 };
  71. struct ColorMap *cm=NULL;
  72.  
  73. struct ViewExtra *vextra=NULL;      /* Extended structures used in Release 2 */
  74. struct MonitorSpec *monspec=NULL;
  75. struct ViewPortExtra *vpextra=NULL;
  76. struct DimensionInfo dimquery = { 0 };
  77.  
  78. UBYTE *displaymem = NULL;     /*  Pointer for writing to BitMap memory.  */
  79.  
  80. #define BLACK 0x000           /*  RGB values for the four colors used.   */
  81. #define RED   0xf00
  82. #define GREEN 0x0f0
  83. #define BLUE  0x00f
  84.  
  85. /*
  86.  * main():  create a custom display; works under either 1.3 or Release 2
  87.  */
  88. VOID main(VOID)
  89. {
  90. WORD depth, box;
  91. struct RasInfo rasInfo;
  92. ULONG modeID;
  93.  
  94. struct TagItem vcTags[] =
  95. {
  96.     {VTAG_ATTACH_CM_SET, NULL },
  97.     {VTAG_VIEWPORTEXTRA_SET, NULL },
  98.     {VTAG_NORMAL_DISP_SET, NULL },
  99.     {VTAG_END_CM, NULL }
  100. };
  101.  
  102. /*  Offsets in BitMap where boxes will be drawn.  */
  103. static SHORT boxoffsets[] = { 802, 2010, 3218 };
  104.  
  105. static UWORD colortable[] = { BLACK, RED, GREEN, BLUE };
  106.  
  107. /* Open the graphics library */
  108. GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 33L);
  109. if(GfxBase == NULL)
  110.     fail("Could not open graphics library\n");
  111.  
  112. /*  Example steals screen from Intuition if Intuition is around.      */
  113. oldview = GfxBase->ActiView;   /* Save current View to restore later. */
  114.  
  115. InitView(&view);           /*  Initialize the View and set View.Modes.     */
  116. view.Modes |= LACE;        /*  This is the old 1.3 way (only LACE counts). */
  117.  
  118. if(GfxBase->LibNode.lib_Version >= 36)
  119.     {
  120.     /* Form the ModeID from values in <displayinfo.h> */
  121.     modeID=DEFAULT_MONITOR_ID | HIRESLACE_KEY;
  122.  
  123.     /*  Make the ViewExtra structure   */
  124.     if( vextra=GfxNew(VIEW_EXTRA_TYPE) )
  125.         {
  126.         /* Attach the ViewExtra to the View */
  127.         GfxAssociate(&view , vextra);
  128.         view.Modes |= EXTEND_VSTRUCT;
  129.  
  130.         /* Create and attach a MonitorSpec to the ViewExtra */
  131.         if( monspec=OpenMonitor(NULL,modeID) )
  132.             vextra->Monitor=monspec;
  133.         else
  134.             fail("Could not get MonitorSpec\n");
  135.         }
  136.      else fail("Could not get ViewExtra\n");
  137.      }
  138.  
  139.  
  140. /*  Initialize the BitMap for RasInfo.  */
  141. InitBitMap(&bitMap, DEPTH, WIDTH, HEIGHT);
  142.  
  143. /* Set the plane pointers to NULL so the cleanup routine */
  144. /* will know if they were used.                          */
  145. for(depth=0; depth<DEPTH; depth++)
  146.     bitMap.Planes[depth] = NULL;
  147.  
  148. /*  Allocate space for BitMap.             */
  149. for (depth=0; depth<DEPTH; depth++)
  150.     {
  151.     bitMap.Planes[depth] = (PLANEPTR)AllocRaster(WIDTH, HEIGHT);
  152.     if (bitMap.Planes[depth] == NULL)
  153.         fail("Could not get BitPlanes\n");
  154.     }
  155.  
  156. rasInfo.BitMap = &bitMap;       /*  Initialize the RasInfo.  */
  157. rasInfo.RxOffset = 0;
  158. rasInfo.RyOffset = 0;
  159. rasInfo.Next = NULL;
  160.  
  161. InitVPort(&viewPort);           /*  Initialize the ViewPort.  */
  162. view.ViewPort = &viewPort;      /*  Link the ViewPort into the View.  */
  163. viewPort.RasInfo = &rasInfo;
  164. viewPort.DWidth = WIDTH;
  165. viewPort.DHeight = HEIGHT;
  166.  
  167. /* Set the display mode the old-fashioned way */
  168. viewPort.Modes=HIRES | LACE;
  169.  
  170. if(GfxBase->LibNode.lib_Version >= 36)
  171. {
  172.     /* Make a ViewPortExtra and get ready to attach it */
  173.     if( vpextra = GfxNew(VIEWPORT_EXTRA_TYPE) )
  174.         {
  175.         vcTags[1].ti_Data = (ULONG) vpextra;
  176.  
  177.         /* Initialize the DisplayClip field of the ViewPortExtra */
  178.         if( GetDisplayInfoData( NULL , (UBYTE *) &dimquery ,
  179.                                 sizeof(dimquery) , DTAG_DIMS, modeID) )
  180.             {
  181.             vpextra->DisplayClip = dimquery.Nominal;
  182.  
  183.             /* Make a DisplayInfo and get ready to attach it */
  184.             if( !(vcTags[2].ti_Data = (ULONG) FindDisplayInfo(modeID)) )
  185.                 fail("Could not get DisplayInfo\n");
  186.              }
  187.         else fail("Could not get DimensionInfo \n");
  188.         }
  189.     else fail("Could not get ViewPortExtra\n");
  190.  
  191.     /* This is for backwards compatibility with, for example,   */
  192.     /* a 1.3 screen saver utility that looks at the Modes field */
  193.     viewPort.Modes = (UWORD) (modeID & 0x0000ffff);
  194.     }
  195.  
  196. /*  Initialize the ColorMap.  */
  197. /*  2 planes deep, so 4 entries (2 raised to the #_planes power).  */
  198. cm = GetColorMap(4L);
  199. if(cm == NULL)
  200.     fail("Could not get ColorMap\n");
  201.  
  202. if(GfxBase->LibNode.lib_Version >= 36)
  203.     {
  204.     /* Get ready to attach the ColorMap, Release 2-style */
  205.     vcTags[0].ti_Data = (ULONG) &viewPort;
  206.  
  207.     /* Attach the color map and Release 2 extended structures */
  208.     if( VideoControl(cm,vcTags) )
  209.         fail("Could not attach extended structures\n");
  210.     }
  211. else
  212.     /* Attach the ColorMap, old 1.3-style */
  213.     viewPort.ColorMap = cm;
  214.  
  215. LoadRGB4(&viewPort, colortable, 4);  /* Change colors to those in colortable. */
  216.  
  217. MakeVPort( &view, &viewPort ); /* Construct preliminary Copper instruction list.    */
  218.  
  219. /* Merge preliminary lists into a real Copper list in the View structure. */
  220. MrgCop( &view );
  221.  
  222. /* Clear the ViewPort */
  223. for(depth=0; depth<DEPTH; depth++)
  224.     {
  225.     displaymem = (UBYTE *)bitMap.Planes[depth];
  226.     BltClear(displaymem, (bitMap.BytesPerRow * bitMap.Rows), 1L);
  227.     }
  228.  
  229. LoadView(&view);
  230.  
  231. /*  Now fill some boxes so that user can see something.          */
  232. /*  Always draw into both planes to assure true colors.          */
  233. for (box=1; box<=3; box++)  /* Three boxes; red, green and blue. */
  234.     {
  235.     for (depth=0; depth<DEPTH; depth++)        /*  Two planes.   */
  236.         {
  237.         displaymem = bitMap.Planes[depth] + boxoffsets[box-1];
  238.         drawFilledBox(box, depth);
  239.         }
  240.     }
  241.  
  242. Delay(10L * TICKS_PER_SECOND);   /*  Pause for 10 seconds.                */
  243. LoadView(oldview);               /*  Put back the old View.               */
  244. WaitTOF();                       /*  Wait until the the View is being     */
  245.                                  /*    rendered to free memory.           */
  246. FreeCprList(view.LOFCprList);    /*  Deallocate the hardware Copper list  */
  247. if(view.SHFCprList)              /*    created by MrgCop().  Since this   */
  248.     FreeCprList(view.SHFCprList);/*    is interlace, also check for a     */
  249.                                  /*    short frame copper list to free.   */
  250. FreeVPortCopLists(&viewPort);    /*  Free all intermediate Copper lists   */
  251.                                  /*    from created by MakeVPort().       */
  252. cleanup(RETURN_OK);              /*  Success.                             */
  253. }
  254.  
  255.  
  256. /*
  257.  * fail():  print the error string and call cleanup() to exit
  258.  */
  259. void fail(STRPTR errorstring)
  260. {
  261. printf(errorstring);
  262. cleanup(RETURN_FAIL);
  263. }
  264.  
  265. /*
  266.  * cleanup():  free everything that was allocated.
  267.  */
  268. VOID cleanup(int returncode)
  269. {
  270. WORD depth;
  271.  
  272. /*  Free the color map created by GetColorMap().  */
  273. if(cm) FreeColorMap(cm);
  274.  
  275. /* Free the ViewPortExtra created by GfxNew() */
  276. if(vpextra) GfxFree(vpextra);
  277.  
  278. /*  Free the BitPlanes drawing area.  */
  279. for(depth=0; depth<DEPTH; depth++)
  280.     {
  281.     if (bitMap.Planes[depth])
  282.         FreeRaster(bitMap.Planes[depth], WIDTH, HEIGHT);
  283.     }
  284.  
  285. /* Free the MonitorSpec created with OpenMonitor() */
  286. if(monspec) CloseMonitor( monspec );
  287.  
  288. /* Free the ViewExtra created with GfxNew() */
  289. if(vextra) GfxFree(vextra);
  290.  
  291. /* Close the graphics library */
  292. CloseLibrary((struct Library *)GfxBase);
  293.  
  294. exit(returncode);
  295. }
  296.  
  297.  
  298. /*
  299.  * drawFilledBox(): create a WIDTH/2 by HEIGHT/2 box of color
  300.  *                  "fillcolor" into the given plane.
  301.  */
  302. VOID drawFilledBox(WORD fillcolor, WORD plane)
  303. {
  304. UBYTE value;
  305. WORD boxHeight, boxWidth, width;
  306.  
  307. /*  Divide (WIDTH/2) by eight because each UBYTE that */
  308. /* is written stuffs eight bits into the BitMap.      */
  309. boxWidth = (WIDTH/2)/8;
  310. boxHeight = HEIGHT/2;
  311.  
  312. value = ((fillcolor & (1 << plane)) != 0) ?  0xff : 0x00;
  313.  
  314. for( ; boxHeight; boxHeight--)
  315.     {
  316.     for(width=0 ; width < boxWidth; width++)
  317.         *displaymem++ = value;
  318.  
  319.     displaymem += (bitMap.BytesPerRow - boxWidth);
  320.     }
  321. }